import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo2 {
    public static void main(String[] args) {
        BlockingQueue<Integer> que = new LinkedBlockingQueue<>();

        Thread t = new Thread(()->{
            while(true) {
                try {
                    int val = que.take();
                    System.out.println("消费元素:" + val);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t.start();

        Thread t1 = new Thread(()->{
            int n = 0;

            while(true) {
                try {
                    que.put(n);
                    System.out.println("生产元素:" + n);
                    n++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();

    }
}
